package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import common.AppResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author quan
 * @Description 功能7：注销功能
 * @Date 2023/7/7 16:25
 */

@WebServlet("/logout")
public class LogOutServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset = utf-8");
        //获取session
        HttpSession session = req.getSession();
        //判断session是否有效
        if(session!=null){
            //销毁session
            session.invalidate();
        }
        resp.getWriter().write(objectMapper.writeValueAsString(AppResult.success("注销成功")));
    }
}
